package com.demo.zhou.sniper.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.demo.zhou.sniper.R;
import com.demo.zhou.sniper.dbmanger.SPManager;
import com.demo.zhou.sniper.module.Yxintiaojian;

import java.util.ArrayList;
import java.util.List;

/**
 * 设置页面
 * Created by zhou on 2017/4/19.
 */

public class SetActivity extends BaseActivity implements View.OnClickListener {
    private EditText set_et_gongzxian, set_et_jshu, set_et_fanc, set_et_wangfan;
    private Spinner set_sp_yinyongz;
    private RadioButton color_rb_xianzt;
    final List<String> yinyongz_data = new ArrayList<>();
    private Button set_bt_return, set_bt_seve;
    private String yinyongz = "黄金分割";
    String primitives;
    String mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("设置");
        setRightBtnVisible(false);
        into();
        Intent i = getIntent();
        primitives = i.getStringExtra("primitives");
        mode = i.getStringExtra("mode");
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_setting;
    }

    public void into() {
        set_et_fanc = (EditText) findViewById(R.id.set_et_fanc);
        set_et_jshu = (EditText) findViewById(R.id.set_et_jshu);
        set_et_wangfan = (EditText) findViewById(R.id.set_et_wangfan);
        set_et_gongzxian = (EditText) findViewById(R.id.set_et_gongzxian);
        set_sp_yinyongz = (Spinner) findViewById(R.id.set_sp_yinyongz);
        set_bt_return = (Button) findViewById(R.id.set_bt_return);
        set_bt_seve = (Button) findViewById(R.id.set_bt_seve);
        color_rb_xianzt = (RadioButton) findViewById(R.id.color_rb_xianzt);
        //获得SharedPreferences对象
        //从数据库中获取到群的数据
        yinyongz_data.add("黄金分割");
        yinyongz_data.add("越级");
        yinyongz_data.add("纵向13");
        yinyongz_data.add("横向12");
        yinyongz_data.add("正向12");
        yinyongz_data.add("防守");
        yinyongz_data.add("向前进");
        initClick();
    }

    public void initClick() {
        set_bt_return.setOnClickListener(this);
        set_bt_seve.setOnClickListener(this);
        List<String> qunDatas = new ArrayList<>();

        set_sp_yinyongz.setAdapter(setAdapter(yinyongz_data));
        set_sp_yinyongz.setSelection(0, true);
        set_sp_yinyongz.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //应用值数据处理
                yinyongz = yinyongz_data.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_bt_return:
                finish();
                break;
            case R.id.set_bt_seve:
                inSp(saveYunxtiaojian());
                break;
            default:
                break;
        }

    }


    public void inSp(Yxintiaojian yxintiaojian) {
        if (yxintiaojian != null) {
            SPManager.getInstance().putInt("tongcmax", yxintiaojian.getTongcmax());
            SPManager.getInstance().putInt("tongcmix", yxintiaojian.getTongcmix());
            SPManager.getInstance().putFloat("jshu", yxintiaojian.getJshu());
            SPManager.getInstance().putInt("gongzxian", yxintiaojian.getGongzxian());
            SPManager.getInstance().putFloat("fanc", yxintiaojian.getFanc());
            SPManager.getInstance().putFloat("wangfanjd", yxintiaojian.getWangfanjd());
            SPManager.getInstance().putString("dzhuangtai", yxintiaojian.getDzhuangtai());
            SPManager.getInstance().putString("yinyongz", yxintiaojian.getYinyongz());
            SPManager.getInstance().putBoolean("ztaixz", yxintiaojian.isZtaixz());
            Intent intent = new Intent(SetActivity.this, SetDetailsActivity.class);
            if (primitives != null) {
                intent.putExtra("primitives", primitives);
                intent.putExtra("mode", mode);
            }
            startActivity(intent);
            finish();
        } else {

        }

    }

    public Yxintiaojian saveYunxtiaojian() {
        Yxintiaojian yxintiaojian = new Yxintiaojian();
        if (!set_et_fanc.getText().toString().equals("") &&
                !set_et_jshu.getText().toString().equals("") &&
                !set_et_gongzxian.getText().toString().equals("") &&
                !set_et_wangfan.getText().toString().equals("")) {
            int gongzx = Integer.parseInt(set_et_gongzxian.getText().toString());

            yxintiaojian.setJshu(Float.parseFloat(set_et_jshu.getText().toString()));
            yxintiaojian.setFanc(Float.parseFloat(set_et_fanc.getText().toString()));
            yxintiaojian.setWangfanjd(Float.parseFloat(set_et_wangfan.getText().toString()));
            yxintiaojian.setGongzxian(gongzx);
            yxintiaojian.setYinyongz(yinyongz);
            if (color_rb_xianzt.isChecked()) {
                yxintiaojian.setZtaixz(false);
            }
            return yxintiaojian;

        } else {
            Toast.makeText(SetActivity.this, "请填写完整的设置条件", Toast.LENGTH_SHORT).show();
            return null;
        }


    }

    public ArrayAdapter setAdapter(List list) {
        final ArrayAdapter<String> yinyongzAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
        return yinyongzAdapter;
    }


    /*
     *左侧按钮
     */
    @Override
    protected void onClickLeft() {
        startActivity(new Intent(SetActivity.this, ChoiceActivity.class));
        finish();
    }

}
